import request from "@/utils/request";
import {DefaultResult, PageObj, PageQueryCommonParams, UserPageQuery, UserVO} from "@/apis/types";
// @ts-ignore
import qs from "qs";

export enum USER_APIS_URL {
    queryByPage = "/core/user/page",
    queryById = "/core/user",
    updateUser="/core/user",
    addUser="/core/user",
    userBindRoles="/core/userRole/user-bind-roles"
}

export const USER_PAGE = (params?: UserPageQuery):Promise<DefaultResult<PageObj<UserVO>>> => {
    return request.get<DefaultResult<PageObj<UserVO>>>({
        url: USER_APIS_URL.queryByPage,
        params: params ?? null
    })
}


export const QUERY_BY_USER_ID = (userId: string): Promise<DefaultResult<UserVO>> => {
    return request.get<DefaultResult<UserVO>>({
        url: `${USER_APIS_URL.queryById}/${userId}`
    })
}

export const UPDATE_USER = (updateUserInfo: any): Promise<DefaultResult<string>> => {
    return request.put<DefaultResult<string>>({
        url: USER_APIS_URL.updateUser,
        data: updateUserInfo
    })
}

/**
 * 添加用户
 *
 * @param userInfo 用户信息
 * @constructor
 */
export const ADD_USER = (userInfo: any) : Promise<DefaultResult<string>> => {
    return request.post<DefaultResult<string>>({
        url: USER_APIS_URL.addUser,
        data: userInfo
    })
}

/**
 * 用户绑定角色
 *
 * @param userId 用户id
 * @param roles 角色id
 * @constructor
 */
export const USER_BIND_ROLES = (userId:string, roles:string[]): Promise<DefaultResult> => {
    let postData = {
        userId: userId,
        roleIds: roles.reduce((prev, current) => prev + "," + current)
    }
    console.log("postData", postData)
    return request.post<DefaultResult>({
        url:USER_APIS_URL.userBindRoles,
        data: qs.stringify(postData),
        headers: {
            "Content-Type": "application/x-www-form-urlencoded"
        }
    })
}
